﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ElfTyping.States.GameState.Effects;
using ElfTyping.Animations;
using Microsoft.Xna.Framework;
using ElfTyping.General;

namespace ElfTyping.States.GameState.PlayerSpells
{
    public class HealSpell : PlayerSpell
    {
        public HealSpell()
        {
            Name = "Heal";
            Description = "Rhys heals Shinon\nover time.";
            ShotCoolDown = 30;
            DeactivationTime = 4;
            WordList = new string[] {"HEALTH", "SOOTHE", "MENDER", "DOCTOR", "MEDICS", "REVIVE", "PHYSIC", "ENERGY", "RENEWS", "REPAIR", "SALVED", "TREATS"};
            Effect = new HealEffect(this);
            IndicatorAnimation = new FireEmblemNormalStanceAnimation(@"Textures\GameState\Healer");
            CardImage = new Animation(@"Textures\Cards\Spell_Heal");
        }

        private class HealEffect : GameEffect
        {
            const float StartTimeOfCast = 1f;
            const float StartTimeOfHeal = 2f;
            const float StopTimeOfText = 4f;
            const float StopTimeOfHeal = 10f;
            const float StopTimeOfHealAnimation = 11f;

            const float HealFrequency = 2f; // seconds between heals, not actually a frequency.
            const int HealStrength = 2; // amount healed per tick

            TextBox TextBox = new TextBox("Heal!");

            Animation HealerAnimation = new FireEmblemFocusStanceAnimation(@"Textures\GameState\Healer");
            Animation CircleEffect = new HealingCircleAnimation();
            Animation SparkleEffect = new HealingSparklesAnimation();            

            bool shouldInitializeBoxes = true;
            Rectangle HealerBox;
            Rectangle HealerSpellBox;
            Rectangle SparkleBox;

            HealSpell Spell;

            public HealEffect(HealSpell spell)
                : base()
            {
                Spell = spell;
                TimeLeft = StopTimeOfHealAnimation;
                CurrentAnimation = Animation.EmptyAnimation;
                DrawBox = Rectangle.Empty;

                FireEmblemPortraitAnimation portrait = new RhysPortrait(false);
                portrait.IsSpeaking = true;
                TextBox.Portrait = portrait;
                TextBox.IsPortraitOnLeft = false;
            }

            void InitializeBoxes()
            {
                HealerBox = Script.GetPlayerDrawBox();
                HealerBox.Offset(HealerBox.Width * 3 / 2, 0);

                HealerSpellBox = HealerBox;
                HealerSpellBox.Offset(0, -HealerBox.Height);

                SparkleBox = Script.GetPlayerDrawBox();
                SparkleBox.Height = SparkleBox.Width * 32 / 52;
                SparkleBox.Offset(0, SparkleBox.Width * 20 / 52);

                shouldInitializeBoxes = false;
            }

            public override void OnEnd()
            {
                Spell.Effect = new HealEffect(Spell);
            }

            public override void Tick(float dt)
            {
                if (shouldInitializeBoxes)
                {
                    InitializeBoxes();
                }

                HealerAnimation.Advance(dt);

                if (TimeElapsed <= StartTimeOfCast && StartTimeOfCast <= TimeElapsed + dt)
                { // Start Casting
                    CurrentAnimation = CircleEffect;
                    DrawBox = HealerSpellBox;
                }
                else if (StartTimeOfHeal <= TimeElapsed && TimeElapsed <= StopTimeOfHeal)
                { // Healing
                    CurrentAnimation = SparkleEffect;
                    DrawBox = SparkleBox;

                    for (float healTime = StartTimeOfHeal; healTime <= StopTimeOfHeal; healTime += HealFrequency)
                    {
                        if (TimeElapsed <= healTime && healTime <= TimeElapsed + dt)
                        { // Trigger a heal
                            Script.HealPlayer(HealStrength);
                            SparkleEffect.Reset();
                        }
                    }
                }

                if (TimeElapsed <= StopTimeOfText)
                {
                    TextBox.Tick(dt);
                }

                base.Tick(dt);
            }

            public override void Draw(General.Camera camera)
            {
                if (TimeElapsed < StartTimeOfHeal)
                {
                    HealerAnimation.FlippedHorizontally = false;
                    HealerAnimation.Draw(camera, HealerBox);                    
                }

                if (TimeElapsed < StopTimeOfText)
                {
                    TextBox.Draw(camera);
                }

                if (CurrentAnimation != null)
                {
                    base.Draw(camera);
                }
            }
        }
    }
}
